# -*- encoding: utf-8 -*-
from odoo import api, fields, models, tools


class StockPickingAddRef(models.Model):
    _inherit = "stock.picking"

    partner_xd_id = fields.Many2one('res.partner', u'下单客户',
                                         related='sale_id.partner_id')
    partner_invoice_id = fields.Many2one('res.partner',u'开票客户',
                                     related='sale_id.partner_invoice_id',
                                     store=True)
    # client_order_ref = fields.Char(u'客户订单号',
    #                                  related='sale_id.client_order_ref',
    #                                  store=True)
    client_order_ref = fields.Char(u'客户订单号', compute='_compute_ref')
    user_id = fields.Many2one('res.users',u'业务员',
                                   related='sale_id.user_id',
                                   store=True)

    @api.multi
    def _compute_ref(self):
        clients_ref = None
        for sp in self:
        # if self.origin:
        #     flag = self.origin.strip()[0:2]
        #     if flag == 'SO':
        #         sale_order = self.env['sale.order'].search(
        #             [('name', '=', self.origin)])
        #         clients_ref = sale_order.client_order_ref
        #         self.client_order_ref = clients_ref
            if sp.origin:
                flag = sp.origin.strip()[0:2]
                if flag == 'SO':
                    sale_order = self.env['sale.order'].search(
                        [('name', '=', sp.origin)])
                    clients_ref = sale_order.client_order_ref
                    sp.client_order_ref = clients_ref
        return
